from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


# 寻道大千

class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "寻道大千"

    def play_alert(self):
        if not self.img_exit("./game/img/寻道大千/play_alert.png", 1):
            self.log_msg("不存在弹窗")
            return False

        if self.img_exit("./game/img/寻道大千/play_state.png", 2):
            self.log_msg("战力下降，点击出售")
            self.img_click("./game/img/寻道大千/play_buy.png")
        else:
            self.log_msg("战力上升，点击收服")
            self.img_click("./game/img/寻道大千/play_get.png")

        if self.img_exit("./game/img/寻道大千/play_alert_confirm.png", 1):
            self.log_msg("存在弹窗确认，点击确认")
            self.img_click("./game/img/寻道大千/play_alert_confirm_1.png", 1)
            self.img_click("./game/img/寻道大千/play_alert_confirm_2.png", 1)
        return True

    def game_alert(self):
        self.log_msg("检查是否存在弹窗")
        self.alert_close_by_file("./game/img/寻道大千")

        while True:
            if self.img_click("./game/img/寻道大千/qian.png", 3):
                self.log_msg("签到成功")
                self.click_bottom()
                self.click_bottom()
                continue
            break

    def handle_main(self):
        self.log_msg("等待进入登陆页面")
        self.img_exit("./game/img/寻道大千/go.png", 100)
        self.log_msg("已进入登陆页面")
        self.game_alert()
        self.log_msg("点击进去游戏")
        self.img_click("./game/img/寻道大千/go.png")

        self.game_alert()

        self.d.sleep(2)
        self.log_msg("开始挑战妖王")
        self.img_click_if_exists("点击挑战", "./game/img/寻道大千/boss01.png", "./game/img/寻道大千/boss02.png")
        self.log_msg("选择妖王")
        self.img_click("./game/img/寻道大千/boss02.png")
        if self.img_click("./game/img/寻道大千/boss03.png"):
            self.log_msg("挑战成功")
            self.click_bottom()
        self.click_bottom()
        self.log_msg("开始挑战妖王-完成")

        self.d.sleep(1)
        self.log_msg("开始砍树")
        # self.img_click_if_exists("点击自动", "./game/img/寻道大千/auto.png", "./game/img/寻道大千/start.png")
        # self.log_msg("点击开始")
        # self.img_click("./game/img/寻道大千/start.png")

        self.log_msg("获取砍树位置")
        r = self.find_img("./game/img/寻道大千/play.png")
        kan_x, kan_y = r.get_data("center")
        while True:
            for i in range(17):
                self.log_msg(f"{i + 1}-砍树[{(kan_x, kan_y)}]")
                for j in range(2):
                    self.d.click(kan_x, kan_y)
                    self.d.sleep(0.1)
                self.d.sleep(2)
                self.play_alert()

            self.game_alert()

            if self.fu_check(2):
                self.log_msg("任务已完成")
                break
        self.d.sleep(2)
        self.get_fu()
        return True
